/**
 * Created by Administrator on 2018/6/8 0008.
 */
const redis = require('redis')
const config = require('../config')
const client = redis.createClient(config.redis)

client.on('error', e => {
    console.log(e)
})
client.on('connect', () => {
    console.log(`连接成功`)
})

class Redis {
    static getVal(key) {
        return new Promise((resolve, reject) => {
            client.get(key, function (error, data) {
                if (error) {
                    reject(error)
                }
                if (data) {
                    Redis.setVal(key, data)
                }
                resolve(data)

            })
        })
    }

    static async setVal(key, val,exprires = 6 * 60 * 60) {
        return new Promise((resolve, reject, ) => {
            client.set(key, val, function (error, data) {
                client.expire(key, exprires)
                if (error) {
                    reject(error)
                }
                resolve(true)
            })
        })

    }
    static async delVal(key,) {
        return new Promise((resolve, reject) => {
            client.del(key, function (error, data) {
                if (error) {
                    reject(error)
                }
                resolve(true)
            })
        })

    }
}
module.exports = Redis;